<template>
  <div>
    <HandleBtn :multipleSelection="multipleSelection" @link_detail='link_detail' @handleDownloadXlsx='handleDownloadXlsx'></HandleBtn>
    <el-table
      :data="currentTableData"
      v-loading="loading"
      size="mini"
      stripe
      style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column label="编号" width="50" align="center">
        <template slot-scope="scope">
          {{scope.row.key}}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" width="100" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="型号" width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.size}}
        </template>
      </el-table-column>
      <el-table-column label="厂家" width="150" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.vender}}
        </template>
      </el-table-column>
      <!-- supplier -->
      <el-table-column label="供应商" width="150" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.supplier}}
        </template>
      </el-table-column>
      <el-table-column label="存放位置" width="200" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.posi}}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag
            size="mini"
            type="info" v-if="scope.row.state==0">
            库存备用
          </el-tag>
          <el-tag
            size="mini"
            type="warning" v-if="scope.row.state==1">
            报废
          </el-tag>
          <el-tag
            size="mini"
            type="success" v-if="scope.row.state==2">
            正常
          </el-tag>
          <el-tag
            size="mini"
            type="danger" v-if="scope.row.state==3">
            维修中
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import HandleBtn from '@/components/handleBtn'
import BooleanControl from '../BooleanControl'
import BooleanControlMini from '../BooleanControlMini'
export default {
  components: {
    BooleanControl,
    BooleanControlMini,
    HandleBtn
  },
  props: {
    tableData: {
      default: () => []
    },
    loading: {
      default: false
    }
  },
  data () {
    return {
      currentTableData: [],
      multipleSelection: [],
      downloadColumns: [
        { label: '编号', prop: 'key' },
        { label: '设备名称', prop: 'name' },
        { label: '型号', prop: 'size' },
        { label: '厂家', prop: 'vender' },
        { label: '存放位置', prop: 'posi' },
        { label: '状态', prop: 'state' },
        { label: '供应商', prop: 'supplier' }
      ]
    }
  },
  watch: {
    tableData: {
      handler (val) {
        this.currentTableData = val
      },
      immediate: true
    }
  },
  methods: {
    link_detail () {
      var id = this.multipleSelection[0].id
      console.log(id)
      this.$router.push(`/resource/management/entryform?id=${id}`)
    },
    handleSwitchChange (val, index) {
      const oldValue = this.currentTableData[index]
      this.$set(this.currentTableData, index, {
        ...oldValue,
        type: val
      })
      // 注意 这里并没有把修改后的数据传递出去 如果需要的话请自行修改
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    downloadDataTranslate (data) {
      return data.map(row => ({
        ...row,
        used: row.used ? '离职' : '在职'
      }))
    },
    handleDownloadXlsx (data) {
      this.$export.excel({
        title: 'D2Admin 表格示例',
        columns: this.data,
        data: this.downloadDataTranslate(data)
      })
        .then(() => {
          this.$message('导出表格成功')
        })
    }
  }
}
</script>
